﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdmLaboratorios.Business;
using AdmLaboratorios.DTO;


namespace AdmLaboratorios
{
    public partial class frmOPSeleccion : Form
    {
        public PartnerDTO Partner { get; set; }
        public OrdenProduccionDTO OP { get; set; }

        public frmOPSeleccion()
        {
            InitializeComponent();
        }

        private void ReloadOrders()
        {
            gv.DataSource = null;
            gvItems.DataSource = null;

            if (Partner != null)
            {
                gv.DataSource = OrdenesProduccion.Consulta(
                    txFiltro.Text,
                    eOrdenProduccionEstado.ACTIVA.ToString(),
                    Partner);
            }
            else
            {
                gv.DataSource = OrdenesProduccion.Consulta(
                    txFiltro.Text,
                    eOrdenProduccionEstado.ACTIVA.ToString());
            }

            lblOrdenes.Text = ((DataTable)gv.DataSource).Rows.Count.ToString();
        }

        private void ReloadItems()
        {
            if (gv.SelectedRows.Count == 1)
            {
                gvItems.DataSource = null;
                gvItems.DataSource = OrdenesProduccion.GetItems((int)gv.SelectedRows[0].Cells["Id"].Value);
                lblItems.Text = ((List<OrdenProduccionItemDTO>)gvItems.DataSource).Count.ToString();
            }
        }

        private void frmOPSeleccion_Load(object sender, EventArgs e)
        {
            ReloadOrders();
            txFiltro.Focus();
        }

        private void txFiltro_TextChanged(object sender, EventArgs e)
        {
            ReloadOrders();
        }

        private void btSeleccionar_Click(object sender, EventArgs e)
        {
            Seleccionar();
        }

        private void Seleccionar()
        {
            if (gv.SelectedRows.Count == 1)
            {
                OP = OrdenesProduccion.GetOp((int)gv.SelectedRows[0].Cells["Id"].Value);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void btCancelar_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void gv_SelectionChanged(object sender, EventArgs e)
        {
            ReloadItems();
        }

        private void gv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Seleccionar();
        }

        private void frmOPSeleccion_Shown(object sender, EventArgs e)
        {
            txFiltro.Focus();
        }
    }
}
